package com.mk.cloud.union;

import com.mk.cloud.common.util.EncyUtils;

import java.util.Map;
import java.util.TreeMap;

public class WechatUtil {

    public static String sign(TreeMap<String,String> params, String appkey) throws Exception{
        if(params.containsKey("sign"))//签名明文组装不包含sign字段
            params.remove("sign");
//        params.put("key", appkey);
        StringBuilder sb = new StringBuilder();
        for(Map.Entry<String, String> entry:params.entrySet()){
            if(entry.getValue()!=null&&entry.getValue().length()>0){
                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        if(sb.length()>0){
            sb.deleteCharAt(sb.length()-1);
        }
		sb.append("&key=").append(appkey);
        System.out.println("::::::::"+sb.toString());
        String sign = EncyUtils.MD5(sb.toString(),"");//记得是md5编码的加签
//		System.out.println(EncyUtils.MD5("appId=wxbf939ce73f92108b&nonceStr=46309689&package=prepay_id=wx14112308043448667786e4471942781683&signType=MD5&timeStamp=1526268188194&key=aa542665017489248c437d1b673809bf",""));
//        params.remove("key");
        return sign;
    }
}
